<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="com.example.pojo.Student" %>
<%@ page import="java.util.List" %>
<%@ page import="org.apache.ibatis.session.SqlSession" %>
<%@ page import="com.example.utils.MybatisUtils" %>
<%@ page import="com.example.dao.StudentMapper" %>
<%@ page import="com.example.pojo.StuClass" %>
<!DOCTYPE html>
<html>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    $(document).ready(function(){
        $.ajax({
            url: 'GetClassesServlet',
            type: 'GET',
            dataType: 'json',
            success: function(data){
                var select = $('#class_name');
                $.each(data, function(index, item) {
                    select.append('<option value="' + item + '">' + item + '</option>');
                });
                console.log(data);
            }
        });
    });
</script>
<head>
    <title>查询学生</title>
    <style>
        /* Add your CSS styles here */
        body {
            font-family: Arial, sans-serif;
            background-color: #f2f2f2;
            margin: 0;
            padding: 0;
        }
        h1 {
            text-align: center;
            color: #333;
        }
        form {
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        label {
            display: block;
            margin-bottom: 10px;
            font-weight: bold;
        }
        input[type="text"], select {
            width: 100%;
            padding: 10px;
            margin-bottom: 15px;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-sizing: border-box;
        }
        input[type="submit"] {
            background-color: #4CAF50;
            color: white;
            padding: 12px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            width: 100%;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
    </style>
</head>

<body>
<h1>查询学生</h1>


<form id="studentForm" action="byname" method="get">
    <label for="stu_id">id：</label>
    <input type="text" id="stu_id" name="stu_id">
    <label for="stu_name">姓名：</label>
    <input type="text" id="stu_name" name="stu_name">

    <label for="stu_no">学号：</label>
    <input type="text" id="stu_no" name="stu_no">

    <label for="class_name">班级：</label>
    <select id="class_name" name="class_name">
        <option value="">请选择班级</option>
    </select>
    <input type="submit" value="查询学生">
</form>

<script>
    document.getElementById("studentForm").onsubmit = function(event) {
        // 获取表单字段的值
        var stuId = document.getElementById("stu_id").value.trim();
        var stuName = document.getElementById("stu_name").value.trim();
        var stuNo = document.getElementById("stu_no").value.trim();

        // 如果 id 和学号不为空，则检查它们是否为整数且长度不超过整数范围
        if (stuId !== "") {
            if (isNaN(stuId) || parseInt(stuId) > 2147483647) { // 2147483647 是 INT_MAX
                alert("id必须是不超过整数范围的整数");
                return false;
            }
        }

        if (stuNo !== "") {
            if (isNaN(stuNo) || parseInt(stuNo) > 2147483647) { // 2147483647 是 INT_MAX
                alert("学号必须是不超过整数范围的整数");
                return false;
            }
        }

        // 如果姓名不为空，则确保它是字符串
        if (stuName !== "") {
            if (!isNaN(stuName)) {
                alert("姓名必须是字符串");
                return false;
            }
        }

        return true;
    };
</script>


<%
    List<Student> studentList = (List<Student>) request.getAttribute("studentList");
    if (studentList != null && !studentList.isEmpty()) {
%>
<h3>查询结果:</h3>

<table>
    <tr>
        <th>姓名</th>
        <th>学号</th>
        <th>班级</th>
        <th>院系</th>
    </tr>
    <%
        SqlSession sqlSession1 = MybatisUtils.getSqlSession();
        StudentMapper mapper1 = sqlSession1.getMapper(StudentMapper.class);
        for (Student student : studentList) {
            StuClass stuClass = mapper1.getClassByClassId(student.getClass_id());
    %>
    <tr>
        <td><%= student.getStu_name() %></td>
        <td><%= student.getStu_no() %></td>
        <td><%= stuClass.getClass_name() %></td>
        <td><%= stuClass.getDepartment() %></td>
    </tr>
    <% } %>
</table>

<% } %>

</body>
</html>
